<?php

##################################################
#
# Copyright (c) 2008 - 2009 ABSoft Company
# Written and Designed by Dang Tin Trung
#
# This file is part of ABSoft Framework
#
##################################################

/**
* Listbox Control
* This control show a list box using Jquery for multi choice selection
* @author: trungdt
* @version: 1.0
* @since: 1.0
* 
* Change logs
* Jan 26, 2011 - trungdt - first create
* 
*/

if (!defined('EXPONENT')) exit('');


require_once(BASE."subsystems/forms/controls/formcontrol.php");

class listboxcontrol extends formcontrol {
	function name() { return "List box control"; }
	function isSimpleControl() { return true; }
	function getFieldDefinition() {
                return array(
			DB_FIELD_TYPE=>DB_DEF_STRING,
			DB_FIELD_LEN=>250,
		);
        }
	
	function listboxcontrol( $data,  $load_js = false) {
		$this->load_js = $load_js;
		$this->data = $data;
	}
	
	function onRegister(&$form) {
		$form->enctype = "multipart/form-data";
	}

	function controlToHTML($name) {
		$html='';
		global $db;
        if ($this->load_js)
		  // init script
		  $html.="<script language='Javascript' src='".URL_FULL."js/jquery_toCheckList/jquery.toChecklist.min.js'></script>
          <link type='text/css' rel='stylesheet' media='screen' href='".URL_FULL."js/jquery_toCheckList/jquery.toChecklist.min.css' />
 ";
		
		$html.="
            <select id='{$name}' name='{$name}' multiple='multiple' class='listbox_control'>";
        foreach ($this->data as $key=>$value)
        {
            $html.="<option value={$value->id} ";
            if (isset($value->checked))
            {
                $html.=" selected='selected' ";
            }
            $html.=">{$value->name}</option>";
        }
        $html.="</select>";

        $html.="
            <script type='text/javascript'>
                $(function() {
                    $('#{$name}').toChecklist({
                        addSearchBox : false,
                        addScrollBar : true,
                        width: 400
                    });
                });
            </script>
        ";
		return $html;
	}

	function form($object) {
                if (!defined("SYS_FORMS")) require_once(BASE."subsystems/forms.php");
                exponent_forms_initialize();

                $form = new form();
                if (!isset($object->identifier)) {
                        $object->identifier = "";
                        $object->caption = "";
                        $object->default = "";
                }
                $i18n = exponent_lang_loadFile('subsystems/forms/controls/textcontrol.php');

                $form->register("identifier",$i18n['identifier'],new textcontrol($object->identifier));
                $form->register("caption",$i18n['caption'], new textcontrol($object->caption));
                $form->register("default",$i18n['default'], new textcontrol($object->default));
                $form->register("submit","",new buttongroupcontrol($i18n['save'],'',$i18n['cancel']));
                return $form;
        }

	function update($values, $object) {
                if ($object == null) $object = new uploadcontrol();
                if ($values['identifier'] == "") {
                        $i18n = exponent_lang_loadFile('subsystems/forms/controls/textcontrol.php');
                        $post = $_POST;
                        $post['_formError'] = $i18n['id_req'];
                        exponent_sessions_set("last_POST",$post);
                        return null;
                }
                $object->identifier = $values['identifier'];
                $object->caption = $values['caption'];
                $object->default = $values['default'];
                return $object;
        }

	function parseData($original_name,$formvalues) {
		$file = $formvalues[$original_name];
		return '<a href="'.URL_FULL.$file.'">'.basename($file).'</a>';
	}
}

?>
